import type { Module, ActionContext } from 'vuex'
import type { AxiosResponse } from 'axios'
import type { ToDoStateType } from './typings'
import {
  getTodoList,
  getDoneList,
  getToCcList,
  getToSubmitList,
  getToDoProcessRecordList,
  getTodoViewOptionButtonList,
  backOrderFlowTask,
  submitOrderFlowTask,
  deleteOrderFlowTask
  // getToDoTypeSelectList
} from '~/api/oam/toDo'
import { getDataDictionaryByCode } from '~/api/global'
import { filterValueEnum } from '~/utils/util'

const toDoStatusSelectList = [
  { key: 'agency', value: '待办' },
  { key: 'done', value: '已办' },
  { key: 'cc', value: '抄送' },
  { key: 'submit', value: '提交' }
]

const toDo: Module<ToDoStateType, ToDoStateType> = {
  namespaced: true,
  state: {
    toDoStatusSelectList,
    toDoStatusEnum: filterValueEnum(
      toDoStatusSelectList,
      'key',
      'value'
    ) as ToDoStateType['toDoStatusEnum'],
    toDoTypeSelectList: [],
    toDoTypeEnum: {}
  },
  getters: {
    toDoStatusSelectList: (state) => state.toDoStatusSelectList,
    toDoStatusEnum: (state) => state.toDoStatusEnum,
    toDoTypeSelectList: (state) => state.toDoTypeSelectList,
    toDoTypeEnum: (state) => state.toDoTypeEnum
  },
  mutations: {
    SET_TODO_TYPE_SELECT_LIST: (state, payload) => {
      state.toDoTypeSelectList = payload
      state.toDoTypeEnum = filterValueEnum(payload, 'dictKey', 'dictValue')
    }
  },
  actions: {
    // 获取 工单待办
    GetTodoList<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getTodoList<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 获取 工单已办
    GetDoneList<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getDoneList<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 获取 工单抄送
    GetToCcList<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getToCcList<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 获取 工单提交
    GetToSubmitList<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getToSubmitList<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 获取工单待办流程记录列表
    GetToDoProcessRecordList<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getToDoProcessRecordList<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },
    // 查询待办查看可操作按钮
    GetTodoViewOptionButtonList<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        getTodoViewOptionButtonList<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 回退任务 驳回、回退
    BackOrderFlowTask<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        backOrderFlowTask<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 提交工单任务/处理工单任务/终结工单任务/工单任务归档/发送工单任务/完成工单任务
    SubmitOrderFlowTask<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        submitOrderFlowTask<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 删除工单任务
    DeleteOrderFlowTask<S, T>(_: ActionContext<S, T>, params: T) {
      return new Promise((resolve, reject) => {
        deleteOrderFlowTask<T>(params)
          .then((res: AxiosResponse) => {
            resolve(res.data)
          })
          .catch((err: AxiosResponse) => {
            reject(err)
          })
      })
    },

    // 待办类型下拉列表
    async GetToDoTypeSelectList<S, T>({ commit }: ActionContext<S, T>) {
      return await getDataDictionaryByCode({ code: 'flow_type' }).then((res: AxiosResponse) => {
        commit('SET_TODO_TYPE_SELECT_LIST', res?.data?.data || [])
        return res?.data
      })
    }
  }
}

export default toDo
